import { marked } from 'marked'
import hljs from 'highlight.js'

/**
 * 设置和配置 Markdown 渲染器
 */
export function setupMarkdownRenderer() {
	const renderer = new marked.Renderer()

	// 自定义代码块渲染
	renderer.code = ({ text, lang }: { text: string; lang?: string }) => {
		if (lang && hljs.getLanguage(lang)) {
			try {
				const highlighted = hljs.highlight(text, { language: lang }).value
				return `<pre><code class="hljs language-${lang}">${highlighted}</code></pre>`
			} catch (error) {
				console.error('代码高亮失败:', error)
			}
		}
		// 默认自动检测语言
		const highlighted = hljs.highlightAuto(text).value
		return `<pre><code class="hljs">${highlighted}</code></pre>`
	}

	return renderer
}

/**
 * 工具消息类型枚举
 */
export const ToolMessageType = {
	// 选择工具阶段
	SELECTING: 'selecting',
	// 工具调用完成阶段
	COMPLETED: 'completed',
} as const

/**
 * 工具类型定义和颜色主题配置
 */
export const ToolThemes = {
	selecting: {
		name: '选择工具',
		icon: '🔧',
		className: 'tool-selecting-msg',
		colors: {
			primary: '#667eea',
			secondary: '#764ba2',
			border: '#4c63d2',
			shadow: 'rgba(102, 126, 234, 0.3)',
		},
	},
	deleteFile: {
		name: '删除文件',
		icon: '🗑️',
		className: 'tool-delete-msg',
		colors: {
			primary: '#ff6b6b',
			secondary: '#ff5722',
			border: '#e53e3e',
			shadow: 'rgba(255, 107, 107, 0.3)',
		},
	},
	readDir: {
		name: '读取目录',
		icon: '🗂️',
		className: 'tool-readdir-msg',
		colors: {
			primary: '#4ecdc4',
			secondary: '#44a08d',
			border: '#38b2ac',
			shadow: 'rgba(78, 205, 196, 0.3)',
		},
	},
	modifyFile: {
		name: '修改文件',
		icon: '🔧',
		className: 'tool-modify-msg',
		colors: {
			primary: '#ffa726',
			secondary: '#fb8c00',
			border: '#f57c00',
			shadow: 'rgba(255, 167, 38, 0.3)',
		},
	},
	readFile: {
		name: '读取文件',
		icon: '📖',
		className: 'tool-read-msg',
		colors: {
			primary: '#42a5f5',
			secondary: '#1e88e5',
			border: '#1976d2',
			shadow: 'rgba(66, 165, 245, 0.3)',
		},
	},
	writeFile: {
		name: '写入文件',
		icon: '✏️',
		className: 'tool-write-msg',
		colors: {
			primary: '#66bb6a',
			secondary: '#4caf50',
			border: '#388e3c',
			shadow: 'rgba(102, 187, 106, 0.3)',
		},
	},
	default: {
		name: '工具调用',
		icon: '✅',
		className: 'tool-default-msg',
		colors: {
			primary: '#11998e',
			secondary: '#38ef7d',
			border: '#0d7377',
			shadow: 'rgba(17, 153, 142, 0.3)',
		},
	},
} as const

/**
 * 工具消息匹配规则配置
 */
export const toolMessagePatterns = [
	{
		// 选择工具消息
		pattern: /<p><strong>🔧 \[选择工具\]<\/strong>\s*(.*?)\s*⚡<\/p>/g,
		replacement: '<p class="tool-selecting-msg"><strong>🔧 [选择工具]</strong> $1</p>',
		type: ToolMessageType.SELECTING,
		toolType: 'selecting',
	},
	{
		// 删除文件
		pattern: /<p><strong>🗑️ \[工具调用\]<\/strong>(.*?)<\/p>/g,
		replacement: '<p class="tool-delete-msg"><strong>🗑️ [工具调用]</strong>$1</p>',
		type: ToolMessageType.COMPLETED,
		toolType: 'deleteFile',
	},
	{
		// 读取目录
		pattern: /<p><strong>🗂️ \[工具调用\]<\/strong>(.*?)<\/p>/g,
		replacement: '<p class="tool-readdir-msg"><strong>🗂️ [工具调用]</strong>$1</p>',
		type: ToolMessageType.COMPLETED,
		toolType: 'readDir',
	},
	{
		// 修改文件
		pattern: /<p><strong>🔧 \[工具调用\]<\/strong>(.*?)<\/p>/g,
		replacement: '<p class="tool-modify-msg"><strong>🔧 [工具调用]</strong>$1</p>',
		type: ToolMessageType.COMPLETED,
		toolType: 'modifyFile',
	},
	{
		// 读取文件
		pattern: /<p><strong>📖 \[工具调用\]<\/strong>(.*?)<\/p>/g,
		replacement: '<p class="tool-read-msg"><strong>📖 [工具调用]</strong>$1</p>',
		type: ToolMessageType.COMPLETED,
		toolType: 'readFile',
	},
	{
		// 写入文件
		pattern: /<p><strong>✏️ \[工具调用\]<\/strong>(.*?)<\/p>/g,
		replacement: '<p class="tool-write-msg"><strong>✏️ [工具调用]</strong>$1</p>',
		type: ToolMessageType.COMPLETED,
		toolType: 'writeFile',
	},
	{
		// 兼容旧格式
		pattern: /<p><strong>✅ \[工具调用\]<\/strong>(.*?)<\/p>/g,
		replacement: '<p class="tool-default-msg"><strong>✅ [工具调用]</strong>$1</p>',
		type: ToolMessageType.COMPLETED,
		toolType: 'default',
	},
] as const

/**
 * 检测消息中是否包含工具调用
 */
export function detectToolMessage(content: string): {
	hasToolMessage: boolean
	toolType: string | null
	isToolSelecting: boolean
	isToolCompleted: boolean
} {
	let hasToolMessage = false
	let toolType: string | null = null
	let isToolSelecting = false
	let isToolCompleted = false

	for (const rule of toolMessagePatterns) {
		// 重置pattern的lastIndex，确保正确匹配
		rule.pattern.lastIndex = 0
		if (rule.pattern.test(content)) {
			hasToolMessage = true
			toolType = rule.toolType

			if (rule.type === ToolMessageType.SELECTING) {
				isToolSelecting = true
			} else if (rule.type === ToolMessageType.COMPLETED) {
				isToolCompleted = true
			}
			break
		}
	}

	return {
		hasToolMessage,
		toolType,
		isToolSelecting,
		isToolCompleted,
	}
}
